package com.sinosoft.brokerchannel.rest;


import com.sinosoft.brokerchannel.base.annotation.MyResponseBody;
import com.sinosoft.brokerchannel.base.common.SystemParamFinal;
import com.sinosoft.brokerchannel.base.rest.AbstractController;
import com.sinosoft.brokerchannel.base.server.LogService;
import com.sinosoft.brokerchannel.base.utile.BusinessException;
import com.sinosoft.brokerchannel.entity.vo.*;
import com.sinosoft.brokerchannel.server.CarQuotePriceService;
import com.sinosoft.brokerchannel.server.impl.IusServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 需求名称：[保险经纪渠道平台]<br/>
 *
 * @author gzj
 * @version V1.0
 * @date 2018-8-21 10:48
 * 计算保费
 */
@Api(value = "services", description = "车险渠道平台保费计算")
@Controller
@RequestMapping("/premum")
public class PremiumController extends AbstractController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private CarQuotePriceService carQuotePriceService;
    @Autowired
    private LogService logService;
    @Autowired
    private IusServiceImpl iusService;
    @RequestMapping(value = "quoteprice", method = RequestMethod.POST, produces = {"application/xml;charset=UTF-8", "application/json;charset=UTF-8"}, consumes = {"application/xml;charset=UTF-8", "application/json;charset=UTF-8"})
    @ApiOperation(value = "保费试算", notes = "保费试算", position = 2)
    @ApiResponses({@ApiResponse(code = 400, message = "失败")})
    @MyResponseBody
    public CarQuotePriceResponse quoteprice(@RequestBody CarQuotePriceRequest request) throws Exception {
        long firstTime = System.currentTimeMillis();
        CarQuotePriceResponse response = null;//响应对象
        String flowid = "";//交易ID
        try {
            //初始化
            super.init("保费试算", request.getRequestHead());
            //记录日志
            flowid = super.beforeTheTransaction(request);
            // add by zhaoqi reason:RM-3935非安心支付平台车险询价与核保金额一致性校验——渠道平台 评审人:李振民 评审日期:018-01-26 date：2018-01-25 begin
            // 设置保费试算金额更新标志1为修改金额。其他为不修改
            if (request != null && request.getRequestBody() != null
                    && request.getRequestBody().getPackageJQVO() != null) {
                request.getRequestBody().getPackageJQVO().setMoneyFreshFlg("1");
            }
            if (request != null && request.getRequestBody() != null
                    && request.getRequestBody().getPackageSYVO() != null) {
                request.getRequestBody().getPackageSYVO().setMoneyFreshFlg("1");
            }
            // add by zhaoqi reason:RM-3935非安心支付平台车险询价与核保金额一致性校验——渠道平台 评审人:李振民 评审日期:018-01-26 date：2018-01-25 end
            //调用服务
            response = carQuotePriceService.carQuotePrice(request);
            super.sucessAction();
        } catch (BusinessException e) {
            logger.info(SystemParamFinal.Exception_LOG, e);
            super.businessExceptionAction(e);
        } catch (Exception e) {
            super.exceptionAction(e);
            logger.info(SystemParamFinal.Exception_LOG, e);
        } finally {
            if (response == null) {
                response = new CarQuotePriceResponse();
            }
            response.setResponseHead(super.getResponseHead());
            //monitorService.quoteprice(response, request, firstTime);
            //回写日志
            super.afterTheTransaction(response, flowid);
            //输出格式化日志-用于日志易提取信息
            //interfaceLogService.channelToChioce(interfaceLogCollect("CarQuotePriceService", request, response, System.currentTimeMillis() - firstTime));
        }

        //打印调用该服务耗时
        logService.timeConsuming("selfInspection-000002", this, "quoteprice", firstTime);

        return response;
    }
    @RequestMapping(value = "/selectInsurer", method = RequestMethod.POST, produces = {"application/xml;charset=UTF-8", "application/json;charset=UTF-8"}, consumes = {"application/xml;charset=UTF-8", "application/json;charset=UTF-8"})
    @ApiOperation(value = "保险公司查询", notes = "保险公司查询")
    @MyResponseBody
    public InsurerQueryResponse selectInsurer(@RequestBody InsurerQueryRequest request) throws Exception {
        InsurerQueryResponse response = new InsurerQueryResponse();
        ;//响应对象
        String flowid = "";//交易ID
        try {
            //初始化
            super.init("Ius列表查询", request.getRequestHead());
            //记录日志
            flowid = super.beforeTheTransaction(request);
            //调用服务
            response = iusService.selectInsurer(request);
            //status的状态：1：成功；0：失败；2：暂无数据
            if ("1".equals(response.getResponseBody().getStatus())) {
                super.sucessAction();
            } else {
                super.errorAction(response.getResponseBody().getMsg());
            }
        } catch (BusinessException e) {
            logger.info(SystemParamFinal.Exception_LOG, e);
            super.businessExceptionAction(e);
        } catch (Exception e) {
            super.exceptionAction(e);
            logger.info(SystemParamFinal.Exception_LOG, e);
        } finally {
            if (response == null) {
                response = new InsurerQueryResponse();
            }
            response.setResponseHead(super.getResponseHead());
            //回写日志
            super.afterTheTransaction(response, flowid);
        }
        return response;
    }
}
